import Taro from '@tarojs/taro';
import { origin, noConsole } from '../config';
import qcloud  from'../bower_components/wafer-client-sdk/index.js';

qcloud.setLoginUrl(`${origin}user/login_from_weapp`);

export default (path, options = { method: 'GET', data: {} }) => {
  console.log('request qcloud request path',path);
  console.log('request qcloud request options',options);
  if (!noConsole) {
    console.log(`${new Date().toLocaleString()}【 M=${path} 】P=${JSON.stringify(options.data)}`);
  }
  const header= { 'Content-Type': 'application/json'}
  const newOptions = {
    login: true,
    url:`${origin}${path}`,
    data: options.data,
    header,
    method: options.method.toUpperCase(),
  }
  return new Promise(function(resolve, reject) {
    console.log('request qcloud request resolve',resolve);
    console.log('request qcloud request resolve',reject);
    qcloud.request({
      ...newOptions,
      success: function (response) {
        console.log('request qcloud request response',response);
        if (response.statusCode >= 200 && response.statusCode < 300) {
          resolve(response.data)
        } else {
          reject(response.data)
        }
      },
      fail: function (err) {
        console.log('request qcloud request response err',err);
      }
    });
  });
}
